package io.github.biezhi.elves.spider;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import io.github.biezhi.elves.model.CompAnnualReports;
import io.github.biezhi.elves.model.CompAnnualReportsJson;
import io.github.biezhi.elves.qcc.model.CompAnalysis;
import io.github.biezhi.elves.qcc.model.CompBasicInfo;
import io.github.biezhi.elves.qcc.model.CompChangeRecord;
import io.github.biezhi.elves.qcc.model.OutboundInvestment;
import io.github.biezhi.elves.service.QccService;

@Component
public class QccCombine {

	@Autowired
	private QccService qccService;
	
	public void jsonCombine() {
		//查询100条数据，并更新合并过来的字段
		List<CompBasicInfo> queryCompBasicInfo = qccService.queryCompBasicInfo(100);
		for (CompBasicInfo compBasicInfo : queryCompBasicInfo) {
			String qccId = compBasicInfo.getQccId();
			String pripid = compBasicInfo.getPripid();
			
			//通过qccId查询其他表的数据 合并成json，插入该表
			//企业基本信息
			CompAnalysis compAnalysis = qccService.queryCompAnalysis(qccId);
			String compBasic = JSONObject.toJSONString(compAnalysis);
			
			//对外投资信息
			List<OutboundInvestment> outboundInvestmentList = qccService.queryOutboundInvestment(qccId);
			String outboundInvestment = JSONArray.toJSONString(outboundInvestmentList);
			
			//企业变更信息
			List<CompChangeRecord> compChangeRecordList = qccService.queryCompChangeRecord(qccId);
			String historicalChange = JSONArray.toJSONString(compChangeRecordList);
			
			//企业年报
			List<CompAnnualReports> compAnnualReports = qccService.queryCompAnnualReports(pripid);
			List<CompAnnualReportsJson> compAnnualReportsJsonList = new ArrayList<CompAnnualReportsJson>();
			if (compAnnualReports !=null) {
				for (CompAnnualReports annualReports : compAnnualReports) {
					CompAnnualReportsJson compAnnualReportsJson = new CompAnnualReportsJson();
					String ancheyear = annualReports.getAncheyear();
					compAnnualReportsJson.setAncheyear(ancheyear);
					compAnnualReportsJson.setCompAnnualReports(annualReports);
					compAnnualReportsJsonList.add(compAnnualReportsJson);
				}
			}
			String annualReports=JSONArray.toJSONString(compAnnualReportsJsonList);
			
			//将这些字段更新到t_comp_basic_info表
			qccService.updateJsonData(compBasic, outboundInvestment, historicalChange, annualReports, qccId);
			
			//更新插入状态
			qccService.updateSaveState(qccId);
		}
		
	}

}
